package com.yxb.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yxb.config.rest.RestResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.UUID;

/**
 * @author
 */
public class ReturnToClientUtil {
	
	public static final Logger log = LoggerFactory.getLogger(ReturnToClientUtil.class);

	/**
	 * 这个返回值为false的，一般在interceptor中用
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	public static void responseFail(String code, String error, HttpServletRequest request, HttpServletResponse response)
			throws  IOException {
		RestResult restResult = RestResult.error(code, error);
		response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
		response.getWriter().write(new ObjectMapper().writeValueAsString(restResult));
		response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
	}

	public static void responseFailParameter( String error, HttpServletRequest request, HttpServletResponse response)
			throws  IOException {
		RestResult restResult = RestResult.error(RestResult.CODE_PARAM_INVALID,error);
		response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
		response.getWriter().write(new ObjectMapper().writeValueAsString(restResult));
		response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
	}

}
